package com.TexasHoldem.ServerData.Quest;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.digester3.Digester;
import org.xml.sax.SAXException;

public class PokerQuestRule
{
	private static String conf = "conf/pokerquest.xml";
	private static PokerQuestRule staticinstance = null;
	
	public Map <Integer,QuestData> every_loginquest = new HashMap<Integer, QuestData>();
	public List <QuestData> every_winquest = new ArrayList<QuestData>();
	public List <QuestData> every_onlinequest = new ArrayList<QuestData>();
	public Map<Integer,QuestImpleMap> quest_factory = new HashMap<Integer, QuestImpleMap>();
	public Map<String,QuestImpleMap> questname_factory = new HashMap<String, QuestImpleMap>();
	
	
	public Map<String, QuestImpleMap> getQuestname_factory()
	{
		return questname_factory;
	}
	public Map<Integer, QuestImpleMap> getQuest_factory()
	{
		return quest_factory;
	}
	public Map <Integer,QuestData>  getEvery_loginquest()
	{
		return every_loginquest;
	}
	public List<QuestData> getEvery_winquest()
	{
		return every_winquest;
	}
	public List<QuestData> getEvery_onlinequest()
	{
		return every_onlinequest;
	}
	public void addQuest_factory(QuestImpleMap questimplemap)
    {
		quest_factory.put(questimplemap.getQuestid(), questimplemap);
		questname_factory.put(questimplemap.getClassname(), questimplemap);
    }
	
	public void addEveryLogin_QuestData(QuestData questdata)
    {
        every_loginquest.put(Integer.parseInt(questdata.getDay()), questdata);
    }
	public void addevery_win_QuestData(QuestData questdata)
    {
        System.out.println("every_winquest_questdata: " + questdata.getAward());
        every_winquest.add(questdata);
    }
	public void addevery_online_QuestData(QuestData questdata)
    {
        System.out.println("every_onlinequest_questdata: " + questdata.getAward());
        every_onlinequest.add(questdata);
    }

	public void Init()
	{
		Digester digester = new Digester();
        digester.setValidating(false);

        digester.addObjectCreate("pokerquestrule", PokerQuestRule.class );
        // instantiate Contact class
        
        digester.addObjectCreate("pokerquestrule/quest_factory/questimple", QuestImpleMap.class );
        digester.addObjectCreate("pokerquestrule/every_loginquest/questdata", QuestData.class );
        digester.addObjectCreate("pokerquestrule/every_win/questdata", QuestData.class );
        digester.addObjectCreate("pokerquestrule/every_online/questdata", QuestData.class );
        
        digester.addSetProperties("pokerquestrule/quest_factory/questimple");
        digester.addSetProperties("pokerquestrule/every_loginquest/questdata");
        digester.addSetProperties("pokerquestrule/every_win/questdata");
        digester.addSetProperties("pokerquestrule/every_online/questdata");
        
        digester.addSetNext("pokerquestrule/quest_factory/questimple","addQuest_factory" );
        digester.addSetNext("pokerquestrule/every_loginquest/questdata","addEveryLogin_QuestData" );
        digester.addSetNext("pokerquestrule/every_win/questdata","addevery_win_QuestData" );
        digester.addSetNext("pokerquestrule/every_online/questdata","addevery_online_QuestData" );
        
        digester.addBeanPropertySetter("pokerquestrule/quest_factory/questimple/questid", "questid");  
        digester.addBeanPropertySetter("pokerquestrule/quest_factory/questimple/name", "name");
        digester.addBeanPropertySetter("pokerquestrule/quest_factory/questimple/classname", "classname");
        digester.addBeanPropertySetter("pokerquestrule/quest_factory/questimple/desc", "desc");
        
        digester.addBeanPropertySetter("pokerquestrule/every_loginquest/questdata/day", "day");  
        digester.addBeanPropertySetter("pokerquestrule/every_loginquest/questdata/award", "award");
        
        digester.addBeanPropertySetter("pokerquestrule/every_win/questdata/wincount", "wincount");  
        digester.addBeanPropertySetter("pokerquestrule/every_win/questdata/award", "award");
        
        digester.addBeanPropertySetter("pokerquestrule/every_online/questdata/onlinemintime", "onlinemintime");  
        digester.addBeanPropertySetter("pokerquestrule/every_online/questdata/award", "award");
        
        
        
        try
		{
			staticinstance = (PokerQuestRule) digester.parse(new File(conf));
		}
		catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch (SAXException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static PokerQuestRule getPokerQuestRule()
	{
		return staticinstance;
	}
	
	
	
	
}
